package com.test.cordova.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by Administrator on 2019/1/17 0017.
 */

public class LoadingView extends View {

    private Paint mTextPaint;
    private int width;
    private int height;
    private Rect mTextRect;
    private Rect mDynamicRect;
    private Paint mPaint;
    private int mCurrentH;

    public LoadingView(Context context) {
        this(context,null);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public LoadingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initTextPaint();
        initPaint();

    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        //设定是否使用图像抖动处理，会使绘制出来的图片颜色更加平滑和饱满，图像更加清晰
        mPaint.setDither(true);
    }

    private void initTextPaint() {
        mTextPaint = new Paint();
        mTextPaint.setStyle(Paint.Style.STROKE);
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(Color.RED);
        mTextPaint.setTypeface(Typeface.DEFAULT);
        mTextPaint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,25,getResources().getDisplayMetrics()));
        mTextPaint.setTextAlign(Paint.Align.CENTER);
        mTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    }



    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        mCurrentH = 0;
        mTextRect = new Rect();
        String t = "Loading ...";
        mTextPaint.getTextBounds(t,0,t.length(),mTextRect);
        int twidth = mTextRect.width();
        int theight = mTextRect.height();
        int left = (width - twidth) >> 1;
        int top = (height - theight) >> 1;
        int right = width - left - twidth;
        int bt = height -top -theight;
        mDynamicRect = new Rect(mTextRect.left,mTextRect.top,mTextRect.right,mTextRect.bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // 存为新图层
        canvas.drawText("Loading ...",width>>1,height>>1,mTextPaint);

        initPaint();
        canvas.drawRect(mDynamicRect,mPaint);
       /* if (mCurrentH <= height){
            mCurrentH += 8;
            mDynamicRect.top = height - mCurrentH;
            postInvalidate();
        }*/

    }
}
